package com.panda.utils;

import com.panda.pojo.Context;
import com.panda.pojo.Document;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class DocumentSlicer {


    private static Pattern pattern = Pattern.compile(

            "(?<=\n|^)" +
                    //文件
                    "( *@File[ \n]*\\{[ \n]*([^{}]+)[ \n]*}[\n ]*\n)?" +

                    //名称,可以带有指令
                    " *((([a-zA-Z][a-zA-Z0-9]*:)+)?[a-zA-Z][a-zA-Z0-9]*)" +

                    //属性
                    "([ \n]*\\([ \n]*(.*)[ \n]*\\))?" +

                    //依赖
                    "([ \n]+on[ \n]+(@?[a-zA-Z][a-zA-Z0-9/]*([\n ]*,[\n ]*@?[a-zA-Z][a-zA-Z0-9/]*)*))?" +

                    //别称
                    "([ \n]+as[ \n]+([a-zA-Z][a-zA-Z0-9]*))?" +

                    //命名空间
                    "([ \n]+in[ \n]+([a-zA-Z][a-zA-Z0-9]*))?"

    );


    static List<Document> splice(String s) {

        Map<String, String> stringStringMap = new HashMap<>();

        //提取字符串后再分割
        s = StringUtils.extract(stringStringMap, s);

        s = StringUtils.removeAnnotation(s);

        if (s == null || s.equals("")) {
            return Collections.emptyList();
        }

        //字符串回填
        s = StringUtils.fillBack(stringStringMap, s);

        List<Document> documents = new ArrayList<>();

        while (true) {

            Matcher matcher = pattern.matcher(s);

            if (matcher.find()) {


                int end = Finder.findBody(s.toCharArray(), matcher.end());

                if (end == -1) {
                    throw new RuntimeException("文档分割失败");
                }

                //文件字符串
                String filesS = matcher.group(2);

                //名称
                String nameS = matcher.group(3);


                //带括号属性字符串
                String rawAttrsS = matcher.group(6);


                //属性字符串
                String attrsS = matcher.group(7);

                //依赖字符串
                String depS = matcher.group(9);

                //依赖的文件也要使用
                String as = matcher.group(12);

                String namespace = matcher.group(14);

                String body = s.substring(matcher.end(), end).trim();

                documents.add(new Document(filesS, nameS, attrsS, rawAttrsS, depS, as, namespace, body));

                s = s.substring(end);

            } else {

                break;
            }

        }

        return documents;

    }

    private static Pattern fileDep = Pattern.compile(" *@File[ \n]*\\{[ \n]*([^{}]+)[ \n]*}[\n ]*\n");


    static void extractFiles(Context context) {

        String text = context.getText();

        Matcher matcher = fileDep.matcher(text);

        while (matcher.find()) {

            Document.Files files = Document.Files.create(matcher.group(1));

            context.getCss().addAll(files.getCss());
            context.getJs().addAll(files.getJs());

            text = text.replace(matcher.group(), "");

        }

        context.setText(text);

    }
}
